package com.child.java.page;

import java.util.List;

/**
 * 分页功能
 * 
 * @author jali
 * @since 2018年5月26日
 * 
 */
public class PageInfo<T> {

	public PageInfo(Integer totalRecord, Integer pageSize, Integer currentPage) {
		this.totalRecord = totalRecord;
		pageSize = 12;
		this.pageSize = pageSize;
		this.currentPage = currentPage;
		if (totalRecord % pageSize == 0) {
			this.totalPage = totalRecord / pageSize;
		} else {
			this.totalPage = totalRecord / pageSize + 1;
		}
		
		this.limitIndex = (currentPage - 1) * pageSize;

		if (totalRecord > 5) {
			if (currentPage > 2) {
				this.startIndex = currentPage - 2;
				this.endIndex = startIndex + 5;
			} else {
				this.startIndex = 1;
				this.endIndex = 5;
			}
			
			if(this.endIndex > this.totalPage){
				this.endIndex = totalPage;
				this.startIndex = endIndex - 5;
			}
		} else {
			this.startIndex = 1;
			this.endIndex = totalRecord;
		}
	}

	// 总记录数（数据库计算）select count(*) from tableName
	private Integer totalRecord;// 需要传入

	// 每页显示条数（自己赋值，页面用户传入）
	private Integer pageSize;// 需要传入

	// 页数 记录数/每页显示条数 totalRecord/pageSize 比如 105条，每页10条记录，105/10 = 10.5，那就是11页
	// Math.ceil((double)totalRecord/pageSize),前台页面控制pageSize不能等于0，PageInfo类也判断需>0
	// 尾页 传入的也是这个数
	private Integer totalPage;

	// 起始页 通过当前页显示，只显示5页能控制，如果当前页是第1,2,3页，则显示1,2,3,4,5 如果超过3，比如4，则显示2,3,4,5,6
	// 如果是最后一页或倒数第二页，则显示 比如第十页， 7,8,9,10,11
	private Integer startIndex;

	// 结束页
	private Integer endIndex;

	// 数据集合
	private List<T> list;

	// 当前页数
	private Integer currentPage;

	// 传入数据库limit的开始值，结束值为开始值加上pageSize
	private Integer limitIndex;

	public Integer getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(Integer totalRecord) {
		this.totalRecord = totalRecord;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public Integer getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}

	public Integer getEndIndex() {
		return endIndex;
	}

	public void setEndIndex(Integer endIndex) {
		this.endIndex = endIndex;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getLimitIndex() {
		return limitIndex;
	}

	public void setLimitIndex(Integer limitIndex) {
		this.limitIndex = limitIndex;
	}

}
